home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Trading on the Edge
/
Trading On The Edge - CD-ROM Toolkit (Wayzata Technology)(2031)(1994).bin
/
pc
/
mac_file
/
vendor_d
/
ga_softw
/
ooga
/
classes.lis
< prev
next >
Wrap
Lisp/Scheme
|
1991-02-03
|
25KB
|
856 lines
;;; -*- Mode:Lisp; Package:OOGA; Base:10; Syntax:COMMON-LISP -*-
#||
RESTRICTED RIGHTS LEGEND
Use, duplication, or disclosure by the Government is subject to
restrictions as set forth in subdivision (b)(3)(ii) of the Rights in
Technical Data and Computer Software Clause at 52.227-7013 of the DOD
FAR Supplement.
TSP (The Software Partnership)
P.O. Box 991
Melrose, MA 02176
Copyright 1990 by Lawrence Davis and Daniel Cerys, all rights reserved.
||#
(in-package :ooga)
;************************************************************
;************************************************************
; BASIC GENETIC ALGORITHM
;************************************************************
;************************************************************
(defvar *GA* nil "The current genetic algorithm")
;;; The Basic Genetic Algorithm Contains three modules that
;;; support most of the action.
(defclass BASIC-GENETIC-ALGORITHM
()
((EVALUATION-MODULE :initarg :evaluation-module
:initform (make-instance 'basic-evaluation-module)
:accessor evaluation-module)
(POPULATION-MODULE :initarg :population-module
:initform (make-instance 'basic-population-module)
:accessor population-module)
(REPRODUCTION-MODULE :initarg :reproduction-module
:initform (make-instance 'basic-reproduction-module)
:accessor reproduction-module)
))
;************************************************************
;************************************************************
; BASIC EVALUATION MODULE
;************************************************************
;************************************************************
;;; The Evaluation Module has a pointer to its GA and an evaluator.
(defclass BASIC-EVALUATION-MODULE
()
((GA :initarg :ga :initform nil :accessor ga)
(EVALUATOR :initarg :evaluator
:initform nil
:accessor evaluator)
))
;;; The evaluator has a pointer to its module.
(defclass EVALUATOR
()
((EVALUATION-MODULE :initarg :evaluation-module
:initform nil :accessor evaluation-module)
))
;************************************************************
;************************************************************
; BASIC POPULATION MODULE
;************************************************************
;************************************************************
;;; The population module has pointers to a number of techniques and its GA.
;;; It also maintains the fitness list (list of fitnesses of population members);
;;; the population-size parameter, the desired-trials parameter, the current index
;;; that notes the number of individuals evaluated at the current point in a run;
;;; and a flag that can be set to stop a run.
;;; All the techniques below have pointers to their modules.
(defclass BASIC-POPULATION-MODULE
(periodic-state-display
performance-statistics-collection
doubly-linked-list)
((GA :initarg :ga :initform nil :accessor ga)
(FITNESS-TECHNIQUE :initarg :fitness-technique
:initform nil
:accessor fitness-technique)
(FITNESS-LIST :initarg :fitness-list :initform nil :accessor fitness-list)
(PARENT-SELECTION-TECHNIQUE
:initarg :parent-selection-technique
:initform nil
:accessor parent-selection-technique)
(REPRESENTATION-TECHNIQUE :initarg :representation-technique
:initform nil
:accessor representation-technique)
(INITIALIZATION-TECHNIQUE :initarg :initialization-technique
:initform nil
:accessor initialization-technique)
(REPRODUCTION-TECHNIQUE :initarg :reproduction-technique
:initform nil
:accessor reproduction-technique)
(DELETION-TECHNIQUE :initarg :deletion-technique
:initform nil
:accessor deletion-technique)
(PARAMETERIZATION-TECHNIQUES
:initarg :parameterization-techniques
:initform nil
:accessor parameterization-techniques)
(POPULATION-SIZE :initarg :population-size :initform nil :accessor population-size)
(DESIRED-TRIALS :initarg :desired-trials :initform nil :accessor desired-trials)
(CURRENT-INDEX :initarg :current-index
:initform 0 :accessor current-index)
(STOP-RUN? :initarg :stop-run? :initform nil :accessor stop-run?)
))
;************************************************************
; PARAMETERIZATION
;;; Parameterization techniques modify parameters of a GA during a run.
(defclass PARAMETERIZATION-TECHNIQUE
()
()
)
;;; Population parameterization techniques have pointers to their
;;; population module.
(defclass POPULATION-PARAMETERIZATION-TECHNIQUE
(parameterization-technique)
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module))
)
(defmethod INITIALIZE-FOR-RUN ((technique population-parameterization-technique))
t)
;;; Interpolate fitness decrement interpolates the value of the
;;; fitness decrement parameter in the fitness technique.
;;; The interpolation interval and interpolation specs contain
;;; default values.
(defclass INTERPOLATE-FITNESS-DECREMENT
(population-parameterization-technique)
((INTERPOLATION-INTERVAL :accessor interpolation-interval
:initarg :interpolation-interval
:initform 50)
(INTERPOLATION-SPECS :accessor interpolation-specs
:initarg :interpolation-specs
:initform '(.2 1.2))
))
;************************************************************
; FITNESS TECHNIQUES
;;; A fitness technique generates the list of population member
;;; fitnesses.
(defclass FITNESS-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;;; Fitness is evaluation uses raw evaluations as fitness.
(defclass FITNESS-IS-EVALUATION
(fitness-technique)
())
;;; Linear normalization uses a linearly-decreasing list of
;;; fitnesses. The starting value is the fitness of the first
;;; population member. The decrement is the different between
;;; successive fitnesses. The minimum value is the smallest
;;; value that may be placed on the list. The values of these
;;; parameters given here are defaults.
(defclass LINEAR-NORMALIZATION
(fitness-technique)
((STARTING-VALUE :initarg :starting-value
:initform 100 :accessor starting-value)
(DECREMENT :initarg :decrement
:initform 1 :accessor decrement)
(MINIMUM-VALUE :initarg :minimum-value
:initform 1 :accessor minimum-value)))
;************************************************************
; PARENT SELECTION TECHNIQUES
;;; A parent selection technique is a technique for choosing a
;;; parent from a population for reproduction.
(defclass PARENT-SELECTION-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;;; Roulette wheel parent selection chooses parents randomly
;;; with chances biased by fitness.
(defclass ROULETTE-WHEEL-PARENT-SELECTION
(parent-selection-technique)
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;************************************************************
; REPRODUCTION TECHNIQUE
;;; A reproduction technique is a technique for managing the
;;; population during reproduction.
(defclass REPRODUCTION-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;;; Generational replacement replaces all members of a
;;; population during a run.
(defclass GENERATIONAL-REPLACEMENT
(reproduction-technique)
())
;;; Generational replaclement with elitism carries the best
;;; member of a population into the next generation unmodified.
(defclass GENERATIONAL-REPLACEMENT-WITH-ELITISM
(generational-replacement)
())
;;; Steady state reproduction involves replacing a few members
;;; of the population during each reproduction event.
(defclass STEADY-STATE
(reproduction-technique)
())
;;; Steady state without duplicates replaces a few members at a
;;; time, but allows no duplicate members in the population. It
;;; stops when the number of duplicates it has found exceeds the
;;; maximum-duplicates parameter.
(defclass STEADY-STATE-WITHOUT-DUPLICATES
(steady-state)
((DUPLICATE-TALLY :initarg :duplicate-tally
:initform 0 :accessor duplicate-tally)
(MAXIMUM-DUPLICATES :initarg :maximum-duplicates
:initform 0 :accessor maximum-duplicates)))
;************************************************************
; DELETION TECHNIQUE
;;; A deletion technique is a technique for deciding which
;;; population members to delete when new members have been
;;; generated.
(defclass DELETION-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;;; Delete all is a technique used with generational
;;; replacement.
(defclass DELETE-ALL
(deletion-technique)
())
;;; Delete last removes the worst member of the population to
;;; make room for a new member.
(defclass DELETE-LAST
(deletion-technique)
())
;************************************************************
; INITIALIZATION TECHNIQUE
;;; An initialization technique is a technique for generating
;;; the initial population of the genetic algorithm. It may use
;;; seed chromosomes provided by another process. When it is
;;; called it builds up a list of initial population members.
;;; When this list is as long as the population size, it
;;; installs it in the population module.
;;; The initialization technique also maintains the population
;;; member class parameter. The GA will create objects of this
;;; class when it makes new population members.
(defclass INITIALIZATION-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
(SEEDS :initarg :seeds :initform nil :accessor seeds)
(INITIAL-POPULATION :initarg :initial-population
:initform nil :accessor initial-population)
(POPULATION-MEMBER-CLASS :initarg :population-member-class
:initform 'population-member
:accessor population-member-class)
))
;;; Random binary initialization generates a population with
;;; random bit strings as chromosomes.
(defclass RANDOM-BINARY-INITIALIZATION
(initialization-technique)
())
;;; Random real number initialization generates an initial
;;; population with randomly generated real numbers in its
;;; chromosomes.
(defclass RANDOM-REAL-NUMBER-INITIALIZATION
(initialization-technique)
())
;;; Random permutation generates an initial population with
;;; permutations of a given list as its chromosomes.
(defclass RANDOM-PERMUTATION
(initialization-technique)
((LIST-TO-PERMUTE :initarg :list-to-permute
:initform nil :accessor list-to-permute)))
;************************************************************
; REPRESENTATION TECHNIQUE
;;; Representation techniques store information about the
;;; chromosomal representation.
(defclass REPRESENTATION-TECHNIQUE
()
((POPULATION-MODULE :initarg :population-module
:initform nil :accessor population-module)
))
;;; A binary representation is a list of bits. The length of
;;; the list is stored here.
(defclass BINARY-REPRESENTATION
(representation-technique)
((BIT-STRING-LENGTH :initarg :bit-string-length
:initform nil :accessor bit-string-length)
))
;;; A real number representation is a list of real numbers.
;;; These are described by specs of the form
;;; ((min max integer?) ... ). The integer? parameter
;;; determines whether the values should be maintained as
;;; integers.
(defclass REAL-NUMBER-REPRESENTATION
(representation-technique)
((REAL-NUMBER-SPECS :initarg :real-number-specs
:initform '((0 4194303 t))
:accessor real-number-specs)
(CHROMOSOME-LENGTH :initarg :chromosome-length
:initform 2
:accessor chromosome-length)))
;;; A permuted list is a permutation of a basic list.
(defclass PERMUTED-LIST
(representation-technique)
())
;************************************************************
; POPULATION MEMBER
;;; POPULATION-MEMBER is the basic member of the genetic
;;; algorithm population. A population member is one member of a
;;; linked list. It has an evaluation, an chromosome that is the
;;; thing the operators and evaluator
;;; work on, a predecessor in the population, a successor in the
;;; population, and an index that matches the
;;; current-index in the population module when
;;; the member is created.
;;; The population is maintained in sorted order by evaluations.
(defclass POPULATION-MEMBER
(doubly-linked-list-element)
((EVALUATION :initarg :evaluation :initform nil :accessor evaluation)
(CHROMOSOME :initarg :chromosome :initform nil :accessor chromosome)
(INDEX :initarg :index :initform nil :accessor index)
(POPULATION-MODULE :initarg :population-module :accessor POPULATION-MODULE)
))
;************************************************************
; STATISTICS COLLECTION
;;; The performance statistics collection object is a component
;;; of the population modules used in the tutorial. The object
;;; periodically stores the evaluation of the best population
;;; member on its performance statistics slot. The storage
;;; interval is a parameter of the object.
(defclass PERFORMANCE-STATISTICS-COLLECTION
()
((PERFORMANCE-STATISTICS :initarg :performance-statistics
:initform nil :accessor performance-statistics)
(PERFORMANCE-STATISTICS-INTERVAL :initarg :performance-statistics-interval
:initform 100 :accessor performance-statistics-interval))
)
;************************************************************
; PERIODIC STATE DISPLAY
;;; The periodic state display object is a component of the
;;; population modules used in the tutorial. When the display
;;; flag is on, this object displays the top n members of the
;;; population. The number to display and frequence of display
;;; are parameters of the object.
(defclass PERIODIC-STATE-DISPLAY
()
((DISPLAY-PERIOD :accessor display-period :initarg :display-period
:initform 200)
(DISPLAY-FLAG :accessor display-flag :initarg :display-flag
:initform t)
(NUMBER-TO-DISPLAY :accessor number-to-display :initarg :number-to-display
:initform 5))
(:documentation
"The PERIODIC-STATE-DISPLAY is used to show the state of the GA at regular
intervals."))
;************************************************************
;************************************************************
; BASIC REPRODUCTION MODULE
;************************************************************
;************************************************************
;;; The reproduction module drives the reproduction process. It
;;; has pointers to its GA, a list of operators, and a list of
;;; operator weights. It includes an operator selection
;;; technique and a list of parameterization techniques.
;;; Default operator weights are for a system with a single
;;; operator.
(defclass BASIC-REPRODUCTION-MODULE
()
((GA :initarg :ga :initform nil :accessor ga)
(OPERATOR-SELECTION-TECHNIQUE :initarg :operator-selection-technique
:initform nil
:accessor operator-selection-technique)
(OPERATOR-LIST :initarg :operator-list
:initform nil
:accessor operator-list)
(PARAMETERIZATION-TECHNIQUES
:initarg :parameterization-techniques
:initform nil
:accessor parameterization-techniques)
(OPERATOR-WEIGHTS :initarg :operator-weights :initform '(100) :accessor operator-weights)
))
;************************************************************
; OPERATOR SELECTION TECHNIQUE
;;; An operator selection technique selects an operator for a
;;; reproduction event.
(defclass OPERATOR-SELECTION-TECHNIQUE
()
((REPRODUCTION-MODULE :initarg :reproduction-module
:initform nil :accessor reproduction-module)
))
;;; Use first operator selects the first operator in the
;;; operator list. This is used for efficiency in the
;;; traditional genetic algorithm, which has only one operator.
(defclass USE-FIRST-OPERATOR
(operator-selection-technique)
())
;;; Roulette wheel operator selection chooses an operator from
;;; the operator list, with selection chances biased by each
;;; operator's corresponding weight.
(defclass ROULETTE-WHEEL-OPERATOR-SELECTION
(operator-selection-technique)
())
;************************************************************
; REPRODUCTION PARAMETERIZATION TECHNIQUES
;;; A reproduction parameterization technique is a technique for
;;; altering the parameters of the reproduction module during a
;;; run.
(defclass REPRODUCTION-PARAMETERIZATION-TECHNIQUE
(parameterization-technique)
((REPRODUCTION-MODULE :initarg :reproduction-module
:initform nil :accessor reproduction-module)
))
;;; Interpolate operator weights modifies the operator weight
;;; list by interpolating between initial and final weights as
;;; the run proceeds. The default interpolation specs are for a
;;; two-operator operator list.
(defclass INTERPOLATE-OPERATOR-WEIGHTS
(reproduction-parameterization-technique)
((INTERPOLATION-INTERVAL :accessor interpolation-interval
:initarg :interpolation-interval
:initform 50)
(INTERPOLATION-SPECS :accessor interpolation-specs
:initarg :interpolation-specs
:initform '((60 40) (30 70)))))
;************************************************************
; OPERATORS
;;; A GA operator is a technique for generating new population
;;; members.
(defclass GA-OPERATOR
()
((REPRODUCTION-MODULE :initarg :reproduction-module
:initform nil :accessor reproduction-module)
))
;;; One point crossover and mutate takes two parents as input
;;; and returns two children. The children are the result of
;;; applying one point crossover to the parents if the crossover
;;; rate test is passed. Each bit of the children is replaced
;;; with a randomly selected bit if the bit mutation rate test
;;; is passed for that bit.
(defclass ONE-POINT-CROSSOVER-AND-MUTATE
(ga-operator)
((BIT-MUTATION-RATE :initarg :bit-mutation-rate
:initform .008
:accessor bit-mutation-rate)
(CROSSOVER-RATE :initarg :crossover-rate
:initform .65
:accessor crossover-rate)
))
;;; One point crossover returns two children that are the result
;;; of applying one point crossover to two parents.
(defclass ONE-POINT-CROSSOVER
(ga-operator)
())
;;; One point crossover returns two children that are the result
;;; of applying two point crossover to two parents.
(defclass TWO-POINT-CROSSOVER
(ga-operator)
())
;;; Binary mutation returns one child that is the result of
;;; applying binary mutation at the bit mutation rate to a
;;; single parent.
(defclass BINARY-MUTATION
(ga-operator)
((BIT-MUTATION-RATE :initarg :bit-mutation-rate
:initform .04
:accessor bit-mutation-rate)))
;;; Uniform list crossover takes two parents and returns two
;;; children that are the result of applying uniform crossover
;;; to the parents.
(defclass UNIFORM-LIST-CROSSOVER
(ga-operator)
())
;;; Random bit string generation takes no parents and returns a
;;; randomly generated bit string. (This operator is used for
;;; comparing comparing the performance of random generation
;;; optimizers with more intelligent ones.)
(defclass RANDOM-BIT-STRING-GENERATION
(ga-operator)
(
))
;;; Real number mutation replaces real numbers on a chromosome
;;; with randomly selected ones if a probability test is passed.
;;; The default mutation rate here is abnormally high; it is set
;;; for the example in the tutorial, with a chromosome
;;; containing only two values. The mutation specs describe the
;;; minimum and maximum values for each field, and whether
;;; values should be integral.
(defclass REAL-NUMBER-MUTATION
(ga-operator)
((MUTATION-RATE :initarg :mutation-rate
:initform .5
:accessor mutation-rate)
(MUTATION-SPECS :initarg :mutation-specs
:initform '((0 4194303 t))
:accessor mutation-specs)))
;;; Real number creep creeps the value of each field on a
;;; real-number chromosome if a probability test is passed. The
;;; rate of creep is a parameter. Specs giving the maximum
;;; amount of the creep and whether to force the result to be an
;;; integer are another parameter of the operator.
(defclass REAL-NUMBER-CREEP
(ga-operator)
((CREEP-RATE :initarg :creep-rate
:initform .7
:accessor creep-rate)
(CREEP-SPECS :initarg :creep-specs
:initform '((12000 t))
:accessor creep-specs)))
;;; Average crossover takes two real-number parents as input and
;;; returns a single child that is the result of averaging the
;;; fields in the parents.
(defclass AVERAGE-CROSSOVER
(ga-operator)
(
))
;;; AVERAGE-REAL-CROSSOVER is like AVERAGE-CROSSOVER except that
;;; it does not round off the result of the average.
(defclass AVERAGE-REAL-CROSSOVER (ga-operator) ())
;;; Uniform order based crossover takes two parents as input and
;;; returns two children that are the result of applying
;;; order-based crossover to the parents.
(defclass UNIFORM-ORDER-BASED-CROSSOVER
(ga-operator)
(
))
;;; Scramble sublist mutation takes one parent as input.
;;; It generates a single child like the parent except that
;;; a randomly-selected sublist of the parent is permuted.
(defclass SCRAMBLE-SUBLIST-MUTATION
(ga-operator)
(
))
;;; Random order generation generates a random permutation of a
;;; base list. (This operator is used to compare populations
;;; of randomly generated permutations with populations
;;; of intelligently generated ones.)
(defclass RANDOM-ORDER-GENERATION
(ga-operator)
(
))
;************************************************************
; OPERATOR ADAPTATION DEFCLASSES
; POPULATION MEMBER CLASSES
;;; A lineage population member maintains pointers to its
;;; parents and children.
(defclass LINEAGE-POPULATION-MEMBER
(population-member)
((CHILDREN :initarg :children
:initform nil :accessor children)
(PARENTS :initarg :parents
:initform nil :accessor parents))
)
;;; An adaptation population member maintains the amount of
;;; delta it has earned, and the amount it has inherited.
(defclass ADAPTATION-POPULATION-MEMBER
(lineage-population-member)
((LOCAL-DELTA :initarg :local-delta
:initform 0 :accessor local-delta)
(INHERITED-DELTA :initarg :inherited-delta
:initform 0 :accessor inherited-delta))
)
; ADAPTATION POPULATION MODULE
;;; A lineage list is a component of a lineage tracking
;;; mechanism.
(defclass LINEAGE-LIST
()
((lineage :initarg :lineage :initform nil :accessor lineage)))
;;; A lineage tracker maintains a lineage list, noting who
;;; created who.
(defclass LINEAGE-TRACKER
(basic-population-module)
((LINEAGE-LIST :initarg :lineage-list
:initform (make-instance 'lineage-list)
:accessor lineage-list)))
;;; An adapative operator module is an object that tracks the
;;; performance of the operators in the reproduction module, and
;;; modifies their weights periodically in accord with their
;;; recently observed performance. This object is a component
;;; of population modules. It maintains a number of parameters
;;; described in detail in the methods file.
(defclass ADAPTIVE-OPERATOR-MODULE
(lineage-tracker)
((ADAPTATION-INTERVAL :initarg :adaptation-interval
:initform 50 :accessor adaptation-interval)
(ADAPTATION-WINDOW :initarg :adaptation-window
:initform 100 :accessor adaptation-window)
(NEXT-ADAPTATION :initarg :next-adaptation
:initform nil :accessor next-adaptation)
(INITIAL-OPERATOR-WEIGHTS :initarg :initial-operator-weights
:initform nil :accessor initial-operator-weights)
(MINIMUM-OPERATOR-WEIGHT :initarg :minimum-operator-weight
:initform 10 :accessor minimum-operator-weight)
(ADAPTIVE-DELTA-AMOUNT :initarg :adaptive-delta-amount
:initform 10 :accessor adaptive-delta-amount)
(INHERITED-DELTA-SCALAR :initarg :inherited-delta-scalar
:initform .9 :accessor inherited-delta-scalar)
(INHERITED-DELTA-GENERATIONS :initarg :inherited-delta-generations
:initform 5 :accessor inherited-delta-generations)
))
;;; The adaptive reproduction module sets pointers from parents to children
;;; and vice verse.
(defclass ADAPTIVE-REPRODUCTION-MODULE
(basic-reproduction-module) ())
;************************************************************
; NAME-GA
;;; A function for debugging use. It is not called by the system.
;;; Do not use NAME-GA if you use variables named g, e, p, or r.
;;; It binds those variables to *ga* and its modules for quick keyboard
;;; reference.
(defun NAME-GA (&optional (ga *ga*))
(declare (special g e p r))
(if (typep ga 'basic-genetic-algorithm)
(setf g ga
e (evaluation-module ga)
p (population-module ga)
r (reproduction-module ga))
(format *standard-output*
"~%~%NAME-GA NEEDS A GENETIC ALGORITHM AS ARGUMENT, ~%OR *GA* MUST BE BOUND TO A GENETIC ALGORITHM.~%~%")))